<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of mailinglist
 *
 * @author hassanali
 */
class mailinglist extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/mailinglist_model');
    }

    public function index() {
        $falid_id = $this->mailinglist_model->is_id('mailinglist');

        $data['count'] = count($falid_id);
        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);
        $data['limit'] = $limit;
        $data['result'] = $this->mailinglist_model->filter();
        $data['pagination'] = $this->admin_pagination('index', count($this->mailinglist_model->filter(true)), $limit);
        $this->themes->render('mailinglist/index', $data);
    }



    public function add() {

        $validation = array(

            array(
                'field' => 'email',
                'label' => lang('label_email'),
                'rules' => 'required|trim|valid_email|is_unique[mailinglist.email]'
            )
            
        );

        $this->form_validation->set_rules($validation);

        if ($this->form_validation->run() == FALSE) {


            $this->themes->render('mailinglist/add');
        } else {

            $email = $this->input->post('email');
            $user_data['email'] = $email;
            $user_data['create_date'] = date('Y-m-d H:i:s');
        


       
            $user_data = $this->mailinglist_model->save($user_data);

            $this->themes->set_message(array('url' => 'mailinglist/', 'type' => 'success', 'message' => 'message_add_success'));
        }
    }

    public function edit() {
        // start function login 

        $id = get_segment(4);
        $id_id = $this->mailinglist_model->is_id('mailinglist', array('id' => $id), 'mailinglist/');
        $data['mailinglist'] = $id_id;
        $validation = array(
            array(
                'field' => 'email',
                'label' => lang('label_email'),
                'rules' => 'required|trim|valid_email'
            )
        );

        $this->form_validation->set_rules($validation);

        if ($this->form_validation->run() == FALSE) {


            $this->themes->render('mailinglist/edit', $data);
        } else {



            $email = $this->input->post('email');
            $user_data['email'] = $email;


    
            $user_data = $this->mailinglist_model->update($user_data, $id);
            $this->themes->set_message(array('url' => 'mailinglist/', 'type' => 'success', 'message' => 'message_edit_success'));
        }
    }

    public function all() {
        $id = $this->input->post('id');
        $doaction = $this->input->post('doaction');
        // valid if no item select 
        $this->empty_id_actionall($id);
        $this->empty_id_actionall($doaction, 'message_select_action_from_dropdown');

        switch ($doaction) {
            case 'create_manager': {
                    $data['controal'] = json_encode($_POST['manager']);
                    $data['mode'] = 'manager';
                    $this->mailinglist_model->update_all($id, $data);
                    $this->themes->set_message(array('url' => 'mailinglist/', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;

            default:
                break;
        }
      //  echo json_encode($_POST['manager']);
    }


    public function csv() {
        header('Content-Type: text/html; charset=UTF-8');

        $newline = "\n";
        $csv = '';
        $data = $this->mailinglist_model->filter(true, 'email');


        $r = 0;
        foreach ($data as $value) {
            $r++;
            $csv.='"' . $value->email . '",';
            $csv = rtrim($csv);
            $csv .= $newline;
            if ($r == 2000) {
                sleep(2);
            }
        }

        $this->load->helper('download');
        $filename = 'MALINGLIST_' . date('d-m-Y-h-i-s') . '.csv';
        force_download($filename, $csv);
    }

    public function delete() {
        $id = get_segment(4);
        $falid_id = $this->mailinglist_model->is_id('mailinglist', array('id' => $id), 'mailinglist/');
        
        $this->mailinglist_model->delete($id,$falid_id->file);
        $this->themes->set_message(array('url' => 'mailinglist/', 'type' => 'success', 'message' => 'message_delete_success'));
    }


}

?>
